package helper

import (
	"encoding/json"
	"fmt"
	"regexp"
	"sort"
	"strconv"
	"strings"
	"time"
)

var sufZeroReg = regexp.MustCompile(`\.?0+$`)

// FloatToString float转成string去除0的小数点
func FloatToString(val float64) string {
	return sufZeroReg.ReplaceAllString(fmt.Sprintf("%.2f", val), "")
}

// InterfaceToString interface 转 string
func InterfaceToString(value interface{}) (ret string) {
	if value == nil {
		return ret
	}

	switch value := value.(type) {
	case float64:
		ret = strconv.FormatFloat(value, 'f', -1, 64)
	case float32:
		ret = strconv.FormatFloat(float64(value), 'f', -1, 64)
	case int:
		ret = strconv.Itoa(value)
	case uint:
		ret = strconv.Itoa(int(value))
	case int8:
		ret = strconv.Itoa(int(value))
	case uint8:
		ret = strconv.Itoa(int(value))
	case int16:
		ret = strconv.Itoa(int(value))
	case uint16:
		ret = strconv.Itoa(int(value))
	case int32:
		ret = strconv.Itoa(int(value))
	case uint32:
		ret = strconv.Itoa(int(value))
	case int64:
		ret = strconv.FormatInt(value, 10)
	case uint64:
		ret = strconv.FormatUint(value, 10)
	case string:
		ret = value
	case []byte:
		ret = string(value)
	default:
		newValue, _ := json.Marshal(value)
		ret = string(newValue)
	}

	return ret
}

// IntSliceToString int切片转成字符串
func IntSliceToString(arr []int) string {
	return strings.Replace(strings.Trim(fmt.Sprint(arr), "[]"), " ", ",", -1)
}

// UintSliceToString uint切片转成字符串
func UintSliceToString(arr []uint) string {
	return strings.Replace(strings.Trim(fmt.Sprint(arr), "[]"), " ", ",", -1)
}

// Uint8SliceToString uint8切片转成字符串
func Uint8SliceToString(arr []uint8) string {
	return strings.Replace(strings.Trim(fmt.Sprint(arr), "[]"), " ", ",", -1)
}

// MapSortToString ..
func MapSortToString(m map[string]string) string {
	arr := []string{}
	keyArr := []string{}
	for k := range m {
		keyArr = append(keyArr, k)
	}
	sort.Strings(keyArr)
	for _, v := range keyArr {
		arr = append(arr, v+"="+m[v])
	}
	return strings.Join(arr, "&")
}

// IfElseString ..
func IfElseString(ret bool, str1, str2 string) string {
	if ret {
		return str1
	}
	return str2
}

// StringUtf8ToUnicode 兼容json_encode字符转义的问题
func StringUtf8ToUnicode(str string) string {
	ret := ""
	for _, r := range str {
		rint := int(r)
		if rint < 128 {
			ret += string(r)
		} else {
			ret += "\\u" + strconv.FormatInt(int64(rint), 16)
		}
	}
	return regexp.MustCompile(`/`).ReplaceAllString(ret, `\/`)
}

// TwoTimeDiff ..
func TwoTimeDiff(t1, t2 time.Time, lang string) (ret string) {
	ctimer := int(t1.Sub(t2).Minutes())
	isCN := lang == "zh"
	if ctimer < 1 {
		if isCN {
			ret = "刚刚"
		} else {
			ret = "now"
		}
	} else if ctimer < 60 {
		ret = strconv.Itoa(ctimer)
		if isCN {
			ret += " 分钟前"
		} else {
			ret += " minutes ago"
		}
	} else if ctimer < 60*24 {
		ret = strconv.Itoa(ctimer / 60)
		if isCN {
			ret += " 小时前"
		} else {
			ret += " hours ago"
		}
	} else if ctimer < 60*24*30 {
		ret = strconv.Itoa(ctimer / (60 * 24))
		if isCN {
			ret += " 天前"
		} else {
			ret += " days ago"
		}
	} else if ctimer < 60*24*30*12 {
		ret = strconv.Itoa(ctimer / (60 * 24 * 30))
		if isCN {
			ret += " 月前"
		} else {
			ret += " months ago"
		}
	} else {
		ret = strconv.Itoa(ctimer / (60 * 24 * 30 * 12))
		if isCN {
			ret += " 年前"
		} else {
			ret += " years ago"
		}
	}
	return
}

// GetDateFormat 获取日期格式, eg: Y-m-d H:i:s
func GetDateFormat(f string) string {
	for k, v := range dateMap {
		f = strings.ReplaceAll(f, k, v)
	}
	return f
}

// DefaultDateFormat 获取的时间格式
func DefaultDateFormat() string {
	return GetDateFormat("Y-m-d H:i:s")
}

// CheckEndString 检查字符串后缀的/
func CheckEndString(str string, exsit bool) string {
	length := len(str)
	if length > 0 {
		if !exsit && str[length-1] == '/' {
			str = str[:length-1]
		} else if exsit && str[length-1] != '/' {
			str = str + "/"
		}
	} else if exsit {
		str = "/"
	}
	return str
}
